home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir42 / gnudbm14.zip / GDBMOPEN.C < prev    next >
C/C++ Source or Header  |  1990-08-24  |  14KB  |  439 lines

  1. /* gdbmopen.c - Open the dbm file and initialize data structures for use. */
  2.  
  3. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  4.     Copyright (C) 1990  Free Software Foundation, Inc.
  5.  
  6.     GDBM is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 1, or (at your option)
  9.     any later version.
  10.  
  11.     GDBM is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with GDBM; see the file COPYING.  If not, write to
  18.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     You may contact the author by:
  21.        e-mail:  phil@wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.         phone:  (206) 676-3035
  27.        
  28. *************************************************************************/
  29.  
  30. /*
  31.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@@ddagsi3.bitnet
  32.  *
  33.  * To this port, the same copying conditions apply as to the
  34.  * original release.
  35.  *
  36.  * IMPORTANT:
  37.  * This file is not identical to the original GNU release!
  38.  * You should have received this code as patch to the official
  39.  * GNU release.
  40.  *
  41.  * MORE IMPORTANT:
  42.  * This port comes with ABSOLUTELY NO WARRANTY.
  43.  *
  44.  * $Header: e:/gnu/gdbm/RCS/gdbmopen.c'v 1.4.0.1 90/08/16 09:22:29 tho Exp $
  45.  */
  46.  
  47. #include <stdio.h>
  48. #include <sys/types.h>
  49. #ifndef MSDOS
  50. #include <sys/file.h>
  51. #endif /* not MSDOS */
  52. #include <sys/stat.h>
  53. #include "gdbmdefs.h"
  54. #include "systems.h"
  55. #include "gdbmerrno.h"
  56.  
  57. extern gdbm_error gdbm_errno;
  58.  
  59.  
  60.  
  61. /* Initialize dbm system.  FILE is a pointer to the file name.  If the file
  62.    has a size of zero bytes, a file initialization procedure is performed,
  63.    setting up the initial structure in the file.  BLOCK_SIZE is used during
  64.    initialization to determine the size of various constructs.  If the value
  65.    is less than 512, the file system blocksize is used, otherwise the value
  66.    of BLOCK_SIZE is used.  BLOCK_SIZE is ignored if the file has previously
  67.    initialized.  If READ_WRITE is set to GDBM_READ the user wants to just
  68.    read the database and any call to dbm_store or dbm_delete will fail. Many
  69.    readers can access the database at the same time.  If READ_WRITE is set to
  70.    GDBM_WRITE, the user wants both read and write access to the database and
  71.    requires exclusive access.  If READ_WRITE is GDBM_WRCREAT, the user wants
  72.    both read and write access to the database and if the database does not
  73.    exist, create a new one.  If READ_WRITE is GDBM_NEWDB, the user want a
  74.    new database created, regardless of whether one existed, and wants read
  75.    and write access to the new database.  Any error detected will cause a 
  76.    return value of null and an approprate value will be in gdbm_errno.  If
  77.    no errors occur, a pointer to the "gdbm file descriptor" will be
  78.    returned. */
  79.    
  80.  
  81. gdbm_file_info *
  82. gdbm_open (file, block_size, read_write, mode, fatal_func)
  83.      char *file;
  84.      int  block_size;
  85.      int  read_write;
  86.      int  mode;
  87.      void (*fatal_func) ();
  88. {
  89.   gdbm_file_info *dbf;        /* The record to return. */
  90.   struct stat file_stat;    /* Space for the stat information. */
  91.   int         len;        /* Length of the file name. */
  92.   LONG        num_bytes;    /* Used in reading and writing. */
  93.   int          lock_val;         /* Returned by the flock call. */
  94.   int          file_block_size;    /* Block size to use for a new file. */
  95.   int           index;        /* Used as a loop index. */
  96.  
  97.   /* Allocate new info structure. */
  98.   dbf = (gdbm_file_info *) malloc (sizeof (gdbm_file_info));
  99.   if (dbf == NULL)
  100.     {
  101.       gdbm_errno = GDBM_MALLOC_ERROR;
  102.       return NULL;
  103.     }
  104.  
  105.   /* Initialize some fields for known values.  This is done so gdbm_close
  106.      will work if called before allocating some structures. */
  107.   dbf->dir  = NULL;
  108.   dbf->bucket = NULL;
  109.   for (index = 0; index < CACHE_SIZE; index++)
  110.     {
  111.       dbf->bucket_cache[index].ca_bucket = NULL;
  112.       dbf->bucket_cache[index].ca_data.dptr = NULL;
  113.     }
  114.   
  115.   /* Save name of file. */
  116.   len = strlen (file);
  117.   dbf->name = (char *) malloc (len + 1);
  118.   if (dbf->name == NULL)
  119.     {
  120.       free (dbf);
  121.       gdbm_errno = GDBM_MALLOC_ERROR;
  122.       return NULL;
  123.     }
  124.   strcpy (dbf->name, file);
  125.  
  126.   /* Initialize the fatal error routine. */
  127.   dbf->fatal_err = fatal_func;
  128.  
  129.   /* Open the file. */
  130.   if (read_write == GDBM_READER)
  131.     {
  132.       dbf->desc = open (dbf->name, O_RDONLY|O_BINARY, 0);
  133.     }
  134.   else if (read_write == GDBM_WRITER)
  135.     {
  136.       dbf->desc = open (dbf->name, O_RDWR|O_BINARY, 0);
  137.     }
  138.   else if (read_write == GDBM_NEWDB)
  139.     {
  140.       dbf->desc = open (dbf->name, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, mode);
  141.       read_write = GDBM_WRITER;
  142.     }
  143.   else
  144.     {
  145.       dbf->desc = open (dbf->name, O_RDWR|O_CREAT|O_BINARY, mode);
  146.       read_write = GDBM_WRITER;
  147.     }
  148.   if (dbf->desc < 0)
  149.     {
  150.       free (dbf->name);
  151.       free (dbf);
  152.       gdbm_errno = GDBM_FILE_OPEN_ERROR;
  153.       return NULL;
  154.     }
  155.  
  156.   /* Get the status of the file. */
  157.   fstat (dbf->desc, &file_stat);
  158.  
  159.   /* Lock the file in the approprate way. */
  160.   if (read_write == GDBM_READER)
  161.     {
  162.       if (file_stat.st_size == 0)
  163.     {
  164.       close (dbf->desc);
  165.       free (dbf->name);
  166.       free (dbf);
  167.       gdbm_errno = GDBM_EMPTY_DATABASE;
  168.       return NULL;
  169.     }
  170.       /* Sets lock_val to 0 for success.  See systems.h. */
  171.       READLOCK_FILE(dbf);
  172.     }
  173.   else
  174.     {
  175.       /* Sets lock_val to 0 for success.  See systems.h. */
  176.       WRITELOCK_FILE(dbf);
  177.     }
  178.   if (lock_val != 0)
  179.     {
  180.       close (dbf->desc);
  181.       free (dbf->name);
  182.       free (dbf);
  183.       if (read_write == GDBM_READER)
  184.     gdbm_errno = GDBM_CANT_BE_READER;
  185.       else
  186.     gdbm_errno = GDBM_CANT_BE_WRITER;
  187.       return NULL;
  188.     }
  189.  
  190.   /* Record the kind of user. */
  191.   dbf->read_write = read_write;
  192.   
  193.   /* Decide if this is a new file or an old file. */
  194.   if (file_stat.st_size == 0)
  195.     {
  196.  
  197.       /* This is a new file.  Create an empty database.  */
  198.  
  199.       /* Start with the blocksize. */
  200.       if (block_size < 512)
  201.     file_block_size = STATBLKSIZE;
  202.       else
  203.     file_block_size = block_size;
  204.  
  205.       /* Get space for the file header. */
  206.       dbf->header = (gdbm_file_header *) malloc (file_block_size);
  207.       if (dbf->header == NULL)
  208.     {
  209.       gdbm_close (dbf);
  210.       gdbm_errno = GDBM_MALLOC_ERROR;
  211.       return NULL;
  212.     }
  213.  
  214.       /* Set the magic number and the block_size. */
  215.       dbf->header->header_magic = 0x13579ace;
  216.       dbf->header->block_size = file_block_size;
  217.      
  218.       /* Create the initial hash table directory.  */
  219.       dbf->header->dir_size = 8 * sizeof (LONG);
  220.       dbf->header->dir_bits = 3;
  221.       while (dbf->header->dir_size < dbf->header->block_size)
  222.     {
  223.       dbf->header->dir_size <<= 1;
  224.       dbf->header->dir_bits += 1;
  225.     }
  226.  
  227.       /* Check for correct block_size. */
  228.       if (dbf->header->dir_size != dbf->header->block_size)
  229.     {
  230.       gdbm_close (dbf);
  231.       gdbm_errno = GDBM_BLOCK_SIZE_ERROR;
  232.       return NULL;
  233.     }
  234.  
  235.       /* Allocate the space for the directory. */
  236.       dbf->dir = (LONG *) malloc (dbf->header->dir_size);
  237.       if (dbf->dir == NULL)
  238.     {
  239.       gdbm_close (dbf);
  240.       gdbm_errno = GDBM_MALLOC_ERROR;
  241.       return NULL;
  242.     }
  243.       dbf->header->dir = dbf->header->block_size;
  244.  
  245.       /* Create the first and only hash bucket. */
  246.       dbf->header->bucket_elems =
  247.     (dbf->header->block_size - sizeof (hash_bucket))
  248.     / sizeof (bucket_element) + 1;
  249.       dbf->header->bucket_size  = dbf->header->block_size;
  250.       dbf->bucket = (hash_bucket *) (alloca (dbf->header->bucket_size));
  251.       if (dbf->bucket == NULL)
  252.     {
  253.       gdbm_close (dbf);
  254.       gdbm_errno = GDBM_MALLOC_ERROR;
  255.       return NULL;
  256.     }
  257.       _gdbm_new_bucket (dbf, dbf->bucket, 0);
  258.       dbf->bucket->av_count = 1;
  259.       dbf->bucket->bucket_avail[0].av_adr = 3*dbf->header->block_size;
  260.       dbf->bucket->bucket_avail[0].av_size = dbf->header->block_size;
  261.  
  262.       /* Set table entries to point to hash buckets. */
  263.       for (index = 0; index < dbf->header->dir_size / sizeof (LONG); index++)
  264.     dbf->dir[index] = 2*dbf->header->block_size;
  265.  
  266.       /* Initialize the active avail block. */
  267.       dbf->header->avail.size
  268.     = ( (dbf->header->block_size - sizeof (gdbm_file_header))
  269.      / sizeof (avail_elem)) + 1;
  270.       dbf->header->avail.count = 0;
  271.       dbf->header->avail.next_block = 0;
  272.       dbf->header->next_block  = 4*dbf->header->block_size;
  273.  
  274.       /* Write initial configuration to the file. */
  275.       /* Block 0 is the file header and active avail block. */
  276. #ifdef MSDOS            /* shut up the compiler!  */
  277.       num_bytes = write (dbf->desc,
  278.              (char *) dbf->header, dbf->header->block_size);
  279. #else /* not MSDOS */
  280.       num_bytes = write (dbf->desc, dbf->header, dbf->header->block_size);
  281. #endif /* not MSDOS */
  282.       if (num_bytes != dbf->header->block_size)
  283.     {
  284.       gdbm_close (dbf);
  285.       gdbm_errno = GDBM_FILE_WRITE_ERROR;
  286.       return NULL;
  287.     }
  288.  
  289.       /* Block 1 is the initial bucket directory. */
  290. #ifdef MSDOS            /* shut up the compiler!  */
  291.       num_bytes = write (dbf->desc, (char *) dbf->dir, dbf->header->dir_size);
  292. #else /* not MSDOS */
  293.       num_bytes = write (dbf->desc, dbf->dir, dbf->header->dir_size);
  294. #endif /* not MSDOS */
  295.       if (num_bytes != dbf->header->dir_size)
  296.     {
  297.       gdbm_close (dbf);
  298.       gdbm_errno = GDBM_FILE_WRITE_ERROR;
  299.       return NULL;
  300.     }
  301.  
  302.       /* Block 2 is the only bucket. */
  303. #ifdef MSDOS            /* shut up the compiler!  */
  304.       num_bytes = write (dbf->desc,
  305.              (char *) dbf->bucket, dbf->header->bucket_size);
  306. #else /* not MSDOS */
  307.       num_bytes = write (dbf->desc, dbf->bucket, dbf->header->bucket_size);
  308. #endif /* not MSDOS */
  309.       if (num_bytes != dbf->header->bucket_size)
  310.     {
  311.       gdbm_close (dbf);
  312.       gdbm_errno = GDBM_FILE_WRITE_ERROR;
  313.       return NULL;
  314.     }
  315.  
  316.       /* Wait for initial configuration to be written to disk. */
  317.       fsync (dbf->desc);
  318.  
  319.     }
  320.   else
  321.     {
  322.       /* This is an old database.  Read in the information from the file
  323.      header and initialize the hash directory. */
  324.  
  325.       gdbm_file_header partial_header;  /* For the first part of it. */
  326.  
  327.       /* Read the partial file header. */
  328. #ifdef MSDOS            /* shut up the compiler!  */
  329.       num_bytes = read (dbf->desc,
  330.             (char *)&partial_header, sizeof (gdbm_file_header));
  331. #else /* not MSDOS */
  332.       num_bytes = read (dbf->desc, &partial_header, sizeof (gdbm_file_header));
  333. #endif /* not MSDOS */
  334.       if (num_bytes != sizeof (gdbm_file_header))
  335.     {
  336.       gdbm_close (dbf);
  337.       gdbm_errno = GDBM_FILE_READ_ERROR;
  338.       return NULL;
  339.     }
  340.  
  341.       /* Is the magic number good? */
  342. #ifdef MSDOS
  343.       if (partial_header.header_magic != 0x13579aceL)
  344. #else /* not MSDOS */
  345.       if (partial_header.header_magic != 0x13579ace)
  346. #endif /* not MSDOS */
  347.     {
  348.       gdbm_close (dbf);
  349.       gdbm_errno = GDBM_BAD_MAGIC_NUMBER;
  350.       return NULL;
  351.     }
  352.  
  353.       /* It is a good database, read the entire header. */
  354.       dbf->header = (gdbm_file_header *) malloc (partial_header.block_size);
  355.       if (dbf->header == NULL)
  356.     {
  357.       gdbm_close (dbf);
  358.       gdbm_errno = GDBM_MALLOC_ERROR;
  359.       return NULL;
  360.     }
  361.       bcopy (&partial_header, dbf->header, sizeof (gdbm_file_header));
  362. #ifdef MSDOS            /* shut up the compiler!  */
  363.       num_bytes = read (dbf->desc, (char *) &dbf->header->avail.av_table[1],
  364.             dbf->header->block_size-sizeof (gdbm_file_header));
  365. #else /* not MSDOS */
  366.       num_bytes = read (dbf->desc, &dbf->header->avail.av_table[1],
  367.             dbf->header->block_size-sizeof (gdbm_file_header));
  368. #endif /* not MSDOS */
  369.       if (num_bytes != dbf->header->block_size-sizeof (gdbm_file_header))
  370.     {
  371.       gdbm_close (dbf);
  372.       gdbm_errno = GDBM_FILE_READ_ERROR;
  373.       return NULL;
  374.     }
  375.     
  376.       /* Allocate space for the hash table directory.  */
  377.       dbf->dir = (LONG *) malloc (dbf->header->dir_size);
  378.       if (dbf->dir == NULL)
  379.     {
  380.       gdbm_close (dbf);
  381.       gdbm_errno = GDBM_MALLOC_ERROR;
  382.       return NULL;
  383.     }
  384.  
  385.       /* Read the hash table directory. */
  386.       num_bytes = lseek (dbf->desc, dbf->header->dir, L_SET);
  387.       if (num_bytes != dbf->header->dir)
  388.     {
  389.       gdbm_close (dbf);
  390.       gdbm_errno = GDBM_FILE_SEEK_ERROR;
  391.       return NULL;
  392.     }
  393.  
  394. #ifdef MSDOS            /* shut up the compiler!  */
  395.       num_bytes = read (dbf->desc, (char *) dbf->dir, dbf->header->dir_size);
  396. #else /* not MSDOS */
  397.       num_bytes = read (dbf->desc, dbf->dir, dbf->header->dir_size);
  398. #endif /* not MSDOS */
  399.       if (num_bytes != dbf->header->dir_size)
  400.     {
  401.       gdbm_close (dbf);
  402.       gdbm_errno = GDBM_FILE_READ_ERROR;
  403.       return NULL;
  404.     }
  405.  
  406.     }
  407.       
  408.   /* Initialize the bucket cache. */
  409.   for (index = 0; index < CACHE_SIZE; index++)
  410.     {
  411.       dbf->bucket_cache[index].ca_bucket
  412.     = (hash_bucket *) malloc (dbf->header->bucket_size);
  413.       if (dbf->bucket_cache[index].ca_bucket == NULL)
  414.     {
  415.       gdbm_close (dbf);
  416.       gdbm_errno = GDBM_MALLOC_ERROR;
  417.       return NULL;
  418.     }
  419.       dbf->bucket_cache[index].ca_adr = 0;
  420.       dbf->bucket_cache[index].ca_changed = FALSE;
  421.       dbf->bucket_cache[index].ca_data.hash_val = -1;
  422.       dbf->bucket_cache[index].ca_data.elem_loc = -1;
  423.     }
  424.  
  425.   /* Finish initializing dbf. */
  426.   dbf->last_read = -1;
  427.   dbf->bucket = dbf->bucket_cache[0].ca_bucket;
  428.   dbf->bucket_dir = 0;
  429.   dbf->cache_entry = &dbf->bucket_cache[0];
  430.   dbf->header_changed = FALSE;
  431.   dbf->directory_changed = FALSE;
  432.   dbf->bucket_changed = FALSE;
  433.   dbf->second_changed = FALSE;
  434.   
  435.   /* Everything is fine, return the pointer to the file information structure.  */
  436.   return dbf;
  437.  
  438. }
  439.